package com.bms.admin.bussiness.dict.web;


import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bms.admin.base.index.model.NowUser;
import com.bms.admin.config.annotation.LoginUser;
import com.bms.auth.annotation.RequiresPermissions;
import com.bms.common.base.common.bean.Result;
import com.bms.common.base.web.BaseController;
import com.bms.common.bussiness.dict.entity.Dict;
import com.bms.common.bussiness.dict.service.IDictService;
import com.feilong.core.Validator;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 * 赛事表 前端控制器
 * </p>
 *
 * @author xj
 * @since 2018-08-15
 */
@RestController
@RequestMapping("dict")
@Api(value="Dict-Api",tags="数据字典接口")
public class DictController extends BaseController{

	@Autowired
	private IDictService dictService;

	@ApiOperation("获取字典（父）列表")
	@GetMapping("list")
	@RequiresPermissions("dict:list")
    public Result list(@ApiIgnore @LoginUser NowUser nowUser,
    				  @RequestParam(defaultValue = "1",required=false)Integer pageNum,
    				  @RequestParam(defaultValue = "20",required=false)Integer pageSize,
    				  String code) {
		Page<Dict> page = dictService.selectPage(new Page<Dict>(pageNum,pageSize),
				new EntityWrapper<Dict>().eq(Validator.isNotNullOrEmpty(code), "code", code).eq("pid", 0));
		page.getRecords().forEach( item -> {
			item.setChildren(dictService.selectList(new EntityWrapper<Dict>().eq("pid", item.getId())));
		});
		return json(page);
    }

	@ApiOperation("获取字典（子）列表")
	@GetMapping("listByPid")
	//@RequiresPermissions("dict:listByPid")
    public Result listByPid(@RequestParam Integer pid) {
		return json(dictService.selectList(new EntityWrapper<Dict>().eq("pid", pid)));
    }

	@GetMapping("select")
	public Result select() {
		return json(dictService.selectList(new EntityWrapper<Dict>().eq("pid", 0)));
	}

	@ApiOperation("新增字典")
	@PostMapping("save")
	@RequiresPermissions("dict:save")
	public Result save(@ApiIgnore @LoginUser NowUser nowUser, @RequestBody Dict dict){
		Date now = new Date();
		dict.setCreateTime(now);
		dict.setCreateUser(nowUser.getUsername());
		dict.setUpdateTime(now);
		dictService.insert(dict);
		return success();
	}

	@ApiOperation("修改字典")
	@PostMapping("update")
	@RequiresPermissions("dict:update")
	public Result update(@ApiIgnore @LoginUser NowUser nowUser,@RequestBody Dict dict){
		dict.setUpdateTime(new Date());
		dict.setUpdateUser(nowUser.getUsername());
		dictService.updateById(dict);
		return success();
	}

	@ApiOperation("删除字典")
	@GetMapping("delete/{dictId}")
	@RequiresPermissions("dict:delete")
    public Result delete(@PathVariable Long dictId) {
		dictService.delete(new EntityWrapper<Dict>().eq("id", dictId).or("pid={0}", dictId));
		return success();
    }
}

